package com.lh_travel.lohas.activity;

import android.animation.Animator;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.lh_travel.lohas.R;
import com.lh_travel.lohas.base.api;
import com.lh_travel.lohas.base.baseActivity;
import com.lh_travel.lohas.domain.CommentIntentBean;
import com.lh_travel.lohas.domain.HotelListBean;
import com.lh_travel.lohas.utils.MapUtil;
import com.lh_travel.lohas.utils.Preferences;
import com.squareup.picasso.Picasso;
import com.tamic.novate.Throwable;
import com.tamic.novate.callback.RxResultCallback;

import java.lang.ref.WeakReference;
import java.util.ArrayList;

import rx.Subscription;

public class HotelLoadingActivity extends baseActivity {

    private String belong;
    private String keyword;
    private String lat;
    private String lng;
    private String checkIn;
    private String checkOut;
    private CommentIntentBean detailBean;
    private ImageView img_gif;
    private RelativeLayout rl_main;
    private Subscription subscription;
    private ImageView img;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_hotel_load);
        findView();
        bindListner();
        ensureUI();
    }

    @Override
    public void findView() {
        img_gif = (ImageView) findViewById(R.id.img_gif);
        rl_main = (RelativeLayout) findViewById(R.id.rl_main);
        img = (ImageView) findViewById(R.id.img);
    }

    @Override
    public void bindListner() {

        rl_main.post(new Runnable() {
            @Override
            public void run() {

                int cx = (rl_main.getLeft() + rl_main.getRight()) / 2;

                int cy = (rl_main.getTop() + rl_main.getBottom()) / 2;

                //起始大小半径
                float startX = 0f;

                float startY = (float) Math.sqrt(cx * cx + cy * cy);
                Animator animator = ViewAnimationUtils.createCircularReveal(rl_main, cx, cy, startX, (float) (rl_main.getWidth() / 1.5));

                animator.setDuration(600);
                animator.start();
            }
        });

    }

    @Override
    public void onBackPressed() {

        if (subscription != null) {
            subscription.unsubscribe();
        }

        int cx = (rl_main.getLeft() + rl_main.getRight()) / 2;

        int cy = (rl_main.getTop() + rl_main.getBottom()) / 2;

        //起始大小半径
        float startX = 0f;

        float startY = (float) Math.sqrt(cx * cx + cy * cy);
        Animator animator = ViewAnimationUtils.createCircularReveal(rl_main, cx, cy, rl_main.getHeight(), startX);

        animator.setDuration(600);

        animator.start();

        animator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {

                rl_main.setVisibility(View.GONE);
                HotelLoadingActivity.super.onBackPressed();
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
    }

    @Override
    public void finish() {

        super.finish();
        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
    }

    @Override
    public void ensureUI() {

        int random = (int) (Math.random() * 10 + 1);

        String number = (random == 10 ? random + "" : "0" + random);

        String imgUrl = "https://lohas-changzhou.oss-cn-hangzhou.aliyuncs.com/app/" + number + ".png";

        Picasso.with(mContext).load(imgUrl).
                placeholder(R.drawable.default750).
                error(R.drawable.default750).
                fit().centerCrop().
                into(img);

        Animation rotateAnimation = AnimationUtils.loadAnimation(mContext, R.anim.rote_load);

        img_gif.startAnimation(rotateAnimation);

        if (mApp.getPreference(Preferences.LOCAL.COUNTRY).equals("cn")) {

            Double latDouble = Double.valueOf(mApp.getPreference(Preferences.LOCAL.LAT));
            Double lngDouble = Double.valueOf(mApp.getPreference(Preferences.LOCAL.LNG));

            lat = MapUtil.gcj02_To_Bd09(latDouble, lngDouble)[0] + "";
            lng = MapUtil.gcj02_To_Bd09(latDouble, lngDouble)[1] + "";

        } else {

            lat = mApp.getPreference(Preferences.LOCAL.LAT);
            lng = mApp.getPreference(Preferences.LOCAL.LNG);

        }

        belong = mApp.getPreference(Preferences.LOCAL.COUNTRY);

        detailBean = (CommentIntentBean) getIntent().getSerializableExtra("detail");

        keyword = detailBean.keyWord;

        checkIn = detailBean.checkIn;

        checkOut = detailBean.checkOut;

        subscription = new api(mContext).getHotelList(checkIn, checkOut, belong, 10, 1, keyword, "0", lat, lng,
                "", "", "", "", "", "", new RxResultCallback<HotelListBean>() {
                    @Override
                    public void onNext(Object tag, int code, String message, HotelListBean response) {


                        Intent intent = new Intent(mContext, HotelListActivity.class);

                        CommentIntentBean bean = new CommentIntentBean();

                        bean.keyWord = keyword;

                        bean.belong = belong;

                        bean.checkIn = checkIn;

                        bean.checkOut = checkOut;

                        intent.putExtra("detail", bean);

                        if (code == 200) {

                            intent.putExtra("empty", false);
                            intent.putExtra("list", response.hotel_list);
                            intent.putExtra("hasmore", response.total_count > 10 ? true : false);
                            intent.putExtra("totoal", response.total_count);

                        } else {

                            intent.putExtra("empty", true);
                        }

                        startActivity(intent);

                        finish();
                    }

                    @Override
                    public void onError(Object tag, Throwable e) {
                        showMessage("请求超时");

                        finish();
                    }

                    @Override
                    public void onCancel(Object tag, Throwable e) {

                    }
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

}
